package com.hainatong.shiphelper.mainhome.fragment;

import android.os.Bundle;
import android.view.View;
import cn.bertsir.zbar.Qr.ScanResult;
import cn.bertsir.zbar.QrManager;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hainatong.shiphelper.App;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.MvvmLazyFragment;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentCommonResultBinding;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.module.warehouse.entity.ShelfUuidBean;
import com.hainatong.shiphelper.module.warehouse.viewmodel.WareHouseViewModel;
import com.hainatong.shiphelper.utils.GsonUtil;

import com.hainatong.shiphelper.widget.LoadingDialog;
import com.hainatong.mvvmbase.rxbus.RxBusBaseMessage;

public class CommonResultFragment extends MvvmLazyFragment<FragmentCommonResultBinding, WareHouseViewModel>
{
    private static String COMMON_RESULT_TYPE = "CommonResultFragment";

    public static int RUKU_SUCCESS_RESULT = 0x71;

    public static int CHUKU_SUCCESS_RESULT = 0x72;

    public static int RETURN_SUCCESS_RESULT = 0x73;

    public static int BROKEN_SUCCESS_RESULT = 0x74;

    private String mResultType ;

    private RxBusBaseMessage rxMsg;

    private LoadingDialog loading;

    public static CommonResultFragment newInstance( String resultType )
    {
        CommonResultFragment resultFragment = new CommonResultFragment();
        Bundle bundle = new Bundle();
        bundle.putString(COMMON_RESULT_TYPE,resultType);
        resultFragment.setArguments(bundle);
        return resultFragment;
    }


    @Override
    protected void initParameters()
    {
        Bundle args = getArguments();
        if( args != null && args.size() > 0)
        {
            mResultType = args.getString(COMMON_RESULT_TYPE , "");

            if( !StringUtils.isEmpty( mResultType ))
            {
                rxMsg = GsonUtil.json2Obj( mResultType ,RxBusBaseMessage.class );
            }
        }
    }

    @Override
    public int getLayoutId()
    {
        return R.layout.fragment_common_result;
    }

    @Override
    public int getBindingVariable()
    {
        return 0;
    }

    @Override
    protected void onRetryBtnClick()
    {
    }

    @Override
    protected void initView(View view)
    {
        loading = new LoadingDialog( getActivity() , getString( R.string.loading ) );
        if(ObjectUtils.isNotEmpty( rxMsg ))
        {
            initViewByRxMsg( rxMsg );
        }
        else
        {

            viewDataBinding.tvResult.setText( "成功添加商品入库" );
            viewDataBinding.btnGoOn.setVisibility( View.VISIBLE );
            viewDataBinding.btnGoOn.setOnClickListener( v -> {

            } );
            viewDataBinding.btnGoBack.setVisibility( View.VISIBLE );
            viewDataBinding.btnGoBack.setOnClickListener( v -> {

            } );
        }

    }

    @Override
    protected void lazyLoad()
    {
        // 拿到 货架信息
        viewModel.getShelfUuidResult().observe( this , result -> {
            loading.hide();
            if( result.isSuccess() )
            {
                if( result.getData() != null )
                {
                    // 跳转 入库  货架
                    ShelfUuidBean shelfUuid = result.getData();
                    ComFragmentActivity.startViewByParam(  getActivity(), Constants.VIEW_TYPE_RUKU ,GsonUtil.obj2json( shelfUuid ),true );
                }
            }
            else
            {
                ToastUtils.showShort( result.getMsg() );
            }
        } );
    }

    private void initViewByRxMsg(RxBusBaseMessage mRxMsg)
    {
        switch ( mRxMsg.getCode() )
        {
            case 0x71:
                initRukuSuccessView();
                break;
            case 0x72:
                initChukuSuccessView();
                break;
            case 0x73:
                initReturnSuccessView();
                break;
            case 0x74:
                initBrokenSuccessView();
                break;
        }
    }

    /**
     * 入库成功
     */
    private void initRukuSuccessView()
    {
        viewDataBinding.ivResultImg.setImageResource(R.mipmap.ic_result_success);
        viewDataBinding.tvResult.setText( "成功添加商品入库" );
        viewDataBinding.btnGoOn.setText("扫描货架继续添加");
        viewDataBinding.btnGoOn.setVisibility( View.VISIBLE );
        viewDataBinding.btnGoOn.setOnClickListener( v -> {
            //扫码   货架 uuid
            startScanQrcode(1);
        } );
        viewDataBinding.btnGoBack.setVisibility( View.VISIBLE );
        viewDataBinding.btnGoBack.setOnClickListener( v -> {
            getActivity().finish();
        } );

    }

    public void startScanQrcode(int type )
    {
        QrManager.getInstance().init( App.getScanQrConfigByType( type ) ).startScan(getActivity(), new QrManager.OnScanResultCallback() {
            @Override
            public void onScanSuccess(ScanResult result)
            {
                // 货架 二维码 uuid
                if( result.getType() == ScanResult.CODE_QR )
                {
                    // 跳转 入库  货架
                    //发请求 获取 货架信息
                    loading.show();
                    viewModel.queryWareHouseShelfInfoByUUid( result.getContent() );
                }
                LogUtils.e(TAG, "货架 UUID: " + result.getContent());
            }
        });
    }

    /**
     *  出库 成功
     */
    private void initChukuSuccessView()
    {
        viewDataBinding.ivResultImg.setImageResource(R.mipmap.ic_result_success);
        viewDataBinding.tvResult.setText( "出库成功" );
        viewDataBinding.btnGoOn.setText("扫描商品继续出库");
        viewDataBinding.btnGoOn.setVisibility( View.VISIBLE );
        viewDataBinding.btnGoOn.setOnClickListener( v -> {
            //结束页面 重新开 一个出库 页面
            getActivity().finish();
            ComFragmentActivity.startView(getActivity(),Constants.VIEW_TYPE_CHUKU);

        } );
        viewDataBinding.btnGoBack.setVisibility( View.VISIBLE );
        viewDataBinding.btnGoBack.setOnClickListener( v -> {
            getActivity().finish();
        } );
    }

    /**
     *  归还
     */
    private void initReturnSuccessView()
    {
        viewDataBinding.ivResultImg.setImageResource(R.mipmap.ic_result_success);
        viewDataBinding.tvResult.setText( "归还成功" );
        viewDataBinding.btnGoOn.setVisibility( View.GONE );

        viewDataBinding.btnGoBack.setVisibility( View.VISIBLE );
        viewDataBinding.btnGoBack.setOnClickListener( v -> {
            getActivity().finish();
        } );
    }

    /**
     *  破损提交
     */
    private void initBrokenSuccessView()
    {
        viewDataBinding.ivResultImg.setImageResource(R.mipmap.ic_result_success);
        viewDataBinding.tvResult.setText( "破损提交成功" );
        viewDataBinding.btnGoOn.setVisibility( View.GONE );

        viewDataBinding.btnGoBack.setVisibility( View.VISIBLE );
        viewDataBinding.btnGoBack.setOnClickListener( v -> {
            getActivity().finish();
        } );
    }


}
